package com.spring.learn.springboot.learn02;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.stereotype.Component;

/**
 * Created by bgt on 2018/5/1.
 * bean动态注册
 *
 * 在没有component时 此实体没有被bean管理
 */
@Component
public class BeanRegistryDemo implements BeanDefinitionRegistryPostProcessor {
    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        //注入10个beanregistryentity
        for (int i = 0; i < 10; i++) {
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(BeanRegistryEntity.class);
            builder.addPropertyValue("name","myname"+i);
            registry.registerBeanDefinition("BeanRegistryEntity" + i, builder.getBeanDefinition());
            System.out.print(".");
        }

    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
